#!/usr/bin/perl

use lib::abs qw( base_lib lib );
use JIP::ToolSet; # load my commonly-used modules in a single import

use Getopt::Long qw( GetOptions );       # CPAN modules
use Pod::Usage qw( pod2usage );          #
use Carp qw( croak );                    #
use File::Spec::Functions qw( catfile ); #
use FindBin qw( $RealBin );              #

use Configurator; # base lib
use Log::Console; #

use Installer::CommandCreator; # searcher custom modules
use DB::Schema;                #

my %API = (
    make_hosts     => 'MakeHosts',
    drop_hosts     => 'DropHosts',
    truncate_hosts => 'TruncateHosts',
    make_files     => 'MakeFiles',
    drop_files     => 'DropFiles',
    truncate_files => 'TruncateFiles',
);

#-- ui -------------------------------------------------------------------------
GetOptions( 'help' => \my $isHelp, 'mode=s' => \my $mode );

pod2usage( -exitstatus => 0, -output => \*STDOUT, -verbose => 2 ) if $isHelp;

pod2usage(
    -exitstatus => 1,
    -output     => \*STDERR,
    -message    => 'required parameter "mode" not defined'
) unless $mode;


croak "unexpected value in --mode" unless $API{ $mode };

#--  log -----------------------------------------------------------------------
our $log = Log::Console->new( log_level => 'all' );

#-- signal handlers ------------------------------------------------------------
require( catfile( $RealBin, 'signal_handlers.pl' ) );

#-- init settings --------------------------------------------------------------
my $conf = Configurator->new( conf => 'robo_conf.pm' )->getSettings;

#-- orm handle -----------------------------------------------------------------
my $orm = DB::Schema->connect(
    $conf->sql->dsn,
    $conf->sql->user,
    $conf->sql->pass,
);

$orm->storage->debug( $conf->sql->isDebug );

#-- main -----------------------------------------------------------------------
$log->Note( q{working with '}. $mode .q{' mode} );

# the Command design pattern
my $CMD = Installer::CommandCreator->create(
    $API{ $mode },
    {
        orm    => $orm,
        log    => $log,
        config => $conf,
    },
);

$CMD->execute;

$log->Note('finish');

#-- end ------------------------------------------------------------------------
__END__

=head1 NAME

make_db.pl - creates database and users for search engine

=head1 SYNOPSIS

make_db.pl [options]

Options:

--help           - display this text

--mode           - work mode

Mode list:

--make_hosts      - make ftpHosts table. remove prev table if exist

--drop_hosts      - drop ftpHosts table if exist

--truncate_hosts  - clean ftpHosts table if exist

--make_files      - make ftpVideoFiles (etc) tables. remove prev table if exist

--drop_files      - drop "files" tables if exist

--truncate_files  - clean "files" tables if exist

=cut
